﻿using NAGFwk.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SilkeyERP.UI.Entities.BuyPriceList
{
    public partial class CustomEditContentView : BaseCustomEditForm
    {
        private Model.BuyPriceList current { get; set; }

        public CustomEditContentView()
        {
            InitializeComponent();
            SetInitialDataSource();
            btnProductSearch.FormChooserType = new Shared.ProductChooser();
            btnProductSearch.OnSelected += btnProductSearch_OnSelected;
            xtcCustomEditContentView.SelectedTabPageIndex = 0;
         
        }

        void btnProductSearch_OnSelected(List<dynamic> SelectedItems)
        {
            AddToBuyPriceListDetails(SelectedItems);
        }

        private void AddToBuyPriceListDetails(List<dynamic> SelectedProducts)
        {            
            var oriResult = ((List<Model.BuyPriceListDetails>)buyPriceListDetailsBindingSource.List);
            foreach (Model.Products SelectedItem in SelectedProducts)
                if (!oriResult.Any(x => x.ProductID == SelectedItem.ProductID))
                    buyPriceListDetailsBindingSource.Add(new Model.BuyPriceListDetails()
                    {
                        BuyPriceListID = current.BuyPriceListID,
                        CurrentPriceWOutTax = 0,
                        CurrentPriceWTax = 0,
                        ProductID = SelectedItem.ProductID,
                        TaxID = 1
                    });
        }

        private void SetInitialDataSource()
        {
            taxBindingSource.DataSource = new SilkeyERP.BL.TaxController().GetAll();
            productsBindingSource.DataSource = new SilkeyERP.BL.ProductsController().GetAll();
            suppliersBindingSource.DataSource = new SilkeyERP.BL.SuppliersController().GetAll();
            taxBindingSource1.DataSource = new SilkeyERP.BL.TaxController().GetAll();
            productsBindingSource1.DataSource = new SilkeyERP.BL.ProductsController().GetAll();
        }

        public void InitData(ref Model.BuyPriceList currentBuyPriceList)
        {
            var _currentBuyPriceListDetails = new List<Model.BuyPriceListDetails>();
            if (currentBuyPriceList.BuyPriceListDetails.Count != 0)
                _currentBuyPriceListDetails.AddRange(currentBuyPriceList.BuyPriceListDetails);
            buyPriceListDetailsBindingSource.DataSource = _currentBuyPriceListDetails;
            currentBuyPriceList.BuyPriceListDetails = _currentBuyPriceListDetails;

            current = currentBuyPriceList;

            if (IsNew)
                current.SetNextAvailableBuyPriceListCode();

            if (IsEdit)
                buyPriceListCodeSpinEdit.Enabled = false;
        }

        private void buyPriceListDetailsGridViw_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            if (((List<Model.BuyPriceListDetails>)buyPriceListDetailsBindingSource.DataSource).Where(x => x.ProductID == ((Model.BuyPriceListDetails)e.Row).ProductID).Count() > 1)
            {
                e.ErrorText = Properties.Resources.ErrorProductDuplicated;
                e.Valid = false;
            }

            var typedBuyPriceListCurrent = ((Model.BuyPriceListDetails)e.Row);

            if (typedBuyPriceListCurrent.TaxID == int.MinValue)
            {
                e.ErrorText = Properties.Resources.ErrorTaxRequired;
                e.Valid = false;
            }

            if (typedBuyPriceListCurrent.ProductID == int.MinValue)
            {
                e.ErrorText = Properties.Resources.ErrorProductRequired;
                e.Valid = false;
            }

            if (typedBuyPriceListCurrent.CurrentPriceWOutTax <= 0)
            {
                e.ErrorText = "Precio sin Impuestos no puede ser Negativo o Cero.";
                e.Valid = false;
            }

            if (typedBuyPriceListCurrent.CurrentPriceWTax <= 0)
            {
                e.ErrorText = "Precio con Impuestos no puede ser Negativo o Cero.";
                e.Valid = false;
            }
        }

        private void rilueTax_EditValueChanged(object sender, EventArgs e)
        {
            if (buyPriceListDetailsBindingSource.Current != null && ((DevExpress.XtraEditors.LookUpEdit)(sender)).EditValue != null && ((DevExpress.XtraEditors.LookUpEdit)(sender)).ItemIndex !=-1)
            {
                var typedPriceListDetailsBindingSource = (Model.BuyPriceListDetails)buyPriceListDetailsBindingSource.Current;
                var SelectedTaxValue = new BL.TaxController().GetTax((int)((DevExpress.XtraEditors.LookUpEdit)(sender)).EditValue).TaxValue;
                typedPriceListDetailsBindingSource.CurrentPriceWTax = typedPriceListDetailsBindingSource.CurrentPriceWOutTax * decimal.Parse("1." + SelectedTaxValue.ToString().Replace(".", ""));
            }

        }

        private decimal GetValueWOutTax(decimal CurrentPriceWTax, decimal CurrentTaxValue)
        {
            return CurrentPriceWTax / decimal.Parse("1." + CurrentTaxValue.ToString().Replace(".", ""));
        }

        private decimal GetValueWTax(decimal CurrentPriceWOutTax, decimal CurrentTaxValue)
        {
            return CurrentPriceWOutTax * decimal.Parse("1." + CurrentTaxValue.ToString().Replace(".", ""));
        }

        private void UpdateCurrentPriceWOutTax(decimal CurrentPriceWTax, int TaxID)
        {
            var typedPriceListDetailsBindingSource = (Model.BuyPriceListDetails)buyPriceListDetailsBindingSource.Current;
            var SelectedTaxValue = new BL.TaxController().GetTax(TaxID).TaxValue;
            typedPriceListDetailsBindingSource.CurrentPriceWOutTax = GetValueWOutTax(CurrentPriceWTax, SelectedTaxValue);
        }

        private void UpdateCurrentPriceWTax(decimal CurrentPriceWOutTax, int TaxID)
        {
            var typedPriceListDetailsBindingSource = (Model.BuyPriceListDetails)buyPriceListDetailsBindingSource.Current;
            var SelectedTaxValue = new BL.TaxController().GetTax(TaxID).TaxValue;
            typedPriceListDetailsBindingSource.CurrentPriceWTax = GetValueWTax(CurrentPriceWOutTax, SelectedTaxValue);
        }

        private void riseCurrentPriceWTax_EditValueChanged(object sender, EventArgs e)
        {
            if (buyPriceListDetailsBindingSource.Current != null)
            {
                var typedPriceListDetailsBindingSource = (Model.BuyPriceListDetails)buyPriceListDetailsBindingSource.Current;
                if (typedPriceListDetailsBindingSource.TaxID != 0)
                    UpdateCurrentPriceWOutTax((decimal)((DevExpress.XtraEditors.SpinEdit)sender).EditValue, typedPriceListDetailsBindingSource.TaxID);
            }
        }

        private void riseCurrentPriceWOutTax_EditValueChanged(object sender, EventArgs e)
        {
            if (buyPriceListDetailsBindingSource.Current != null)
            {
                var typedPriceListDetailsBindingSource = (Model.BuyPriceListDetails)buyPriceListDetailsBindingSource.Current;
                if (typedPriceListDetailsBindingSource.TaxID != 0)
                    UpdateCurrentPriceWTax((decimal)((DevExpress.XtraEditors.SpinEdit)sender).EditValue, typedPriceListDetailsBindingSource.TaxID);
            }
        }
    }
}
